<template>
  <!-- 操作栏 -->
  <el-row justify="end">
    <el-button @click="openForm">
      <Icon class="mr-5px" icon="clarity:contract-line" />
      绑定物料
    </el-button>
  </el-row>
  <!-- 列表 align="center" -->
  <ContentWrap class="mt-10px">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" type="index" width="80" />
      <el-table-column label="物料名称" align="center" prop="componentsName" />
      <el-table-column label="物料代码" align="center" prop="componentsNo" />
      <el-table-column label="物料规格" align="center" prop="dimension" />
      <el-table-column label="使用数量" align="center" prop="count" >
        <template #default="scope">
          {{ scope.row.count }}
          <dict-tag :type="DICT_TYPE.CRM_PRODUCT_UNIT" :value="scope.row.componentsUnit" />
        </template>
      </el-table-column>
      <el-table-column label="物料属性" align="center" prop="componentsAttributes" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_COMPONENT_ATTRIBUTES" :value="scope.row.componentsAttributes" />
        </template>
      </el-table-column>
      <el-table-column label="配置属性" align="center" prop="allPurpose" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_ALL_purpose" :value="scope.row.allPurpose" />
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />

      <el-table-column label="操作" align="center" fixed="right" width="160">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="editForm(scope.row.id)"
            v-hasPermi="['crm:product:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crm:product:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加 -->
  <ProductItemsForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import ProductItemsForm from './ProductItemsForm.vue'
import * as ProductApi from '@/api/crm/product'
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE } from '@/utils/dict'
import { ProductComponentsApi, ProductComponents } from '@/api/crm/productcomponents'

defineOptions({ name: 'CrmProductList' })
const props = defineProps<{
  bizType: number // 业务类型
  bizId: number // 业务编号
  customerId: number // 业务编号
}>()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const customerId = computed(() => {
  return props.customerId;
});
const id = computed(() => {
  return props.bizId;
});

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  productId: undefined as unknown // 允许 undefined + number
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // 置空参数
    queryParams.productId = undefined
    queryParams.productId = props.bizId
    // 执行查询
    let data = { list: [], total: 0 }
    data = await ProductComponentsApi.getProductComponentsPage(queryParams)
    if(data){
      list.value = data.list
      total.value = data.total
    }
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加 */
const formRef = ref()
const openForm = () => {debugger
  formRef.value.open('create','',props.customerId,props.bizId)
}
const editForm = (id) => {
  formRef.value.open('update',id,props.customerId,props.bizId)
}
const { currentRoute, push } = useRouter()
const openDetail = (id: number) => {
  push({ name: 'CrmProductDetail', params: { id } })
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductComponentsApi.deleteProductComponents(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
/** 监听打开的 bizId + bizType，从而加载最新的列表 */
watch(
  () => [props.bizId, props.bizType],
  () => {
    handleQuery()
  },
  { immediate: true, deep: true }
)
</script>
